package com.lb.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * \* @author: hasee
 * \* Date: 2024/5/23
 * \* Time: 21:52
 * \* Description:
 * \
 */

@Aspect
public class TransactionAspect {
    @Autowired
    private PlatformTransactionManager transactionManager;

    @Pointcut("execution(* com.lb.service.*.*(..))")
    public void transactionPointcut() {
    }

    @Around("transactionPointcut()")
    public Object doTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            Object result = joinPoint.proceed();
            transactionManager.commit(status);
            return result;
        } catch (Exception e) {
            transactionManager.rollback(status);
            throw e;
        }
    }
}